package cn.liyicun.cp.cpweb.service.money;

import cn.hutool.core.util.IdUtil;
import cn.liyicun.cp.cpweb.base.YesNoEnum;
import cn.liyicun.cp.cpweb.entity.*;
import cn.liyicun.cp.cpweb.mapper.MoneyLogMapper;
import cn.liyicun.cp.cpweb.mapper.MoneyWithdrawMapper;
import cn.liyicun.cp.cpweb.service.BaseService;
import cn.liyicun.cp.cpweb.service.UserMoneyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class WithdrawService extends BaseService implements IMoneyLog {
    @Autowired
    private MoneyLogMapper moneyLogMapper;

    @Autowired
    private MoneyWithdrawMapper moneyWithdrawMapper;

    @Autowired
    private UserMoneyService userMoneyService;

    public PageInfo<MoneyWithdraw> search(Long uid, int currentPage, int pageSize) {
        MoneyWithdrawExample example = new MoneyWithdrawExample();
        if (uid != null && uid > 0) {
            example.createCriteria().andUidEqualTo(uid);
        }
        example.setOrderByClause("id desc");
        PageHelper.startPage(currentPage, pageSize);
        List<MoneyWithdraw> docs = moneyWithdrawMapper.selectByExample(example);
        PageInfo<MoneyWithdraw> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }



    @Override
    public boolean ext(MoneyTypeEnum type, Long id, YesNoEnum status, String remark, Object param) {
        if (type.compareTo(MoneyTypeEnum.withdraw) == 0) {
            withdraw(type, (MoneyWithdraw) param);
            return true;
        } else if (type.compareTo(MoneyTypeEnum.withdraw_apply) == 0) {
            withdrawApply(type, id, status, remark);
            return true;
        }

        return false;
    }

    private void withdrawApply(MoneyTypeEnum type, Long id, YesNoEnum status, String remark) {

        if (status.compareTo(YesNoEnum.yes) == 0) {
            //提现成功
            MoneyWithdraw rec = new MoneyWithdraw();
            rec.setRemark(remark);
            rec.setStatus(1);
            rec.setId(id);
            rec.setUpdateTime(new Date());
            moneyWithdrawMapper.updateByPrimaryKeySelective(rec);
        } else if (status.compareTo(YesNoEnum.no) == 0) {
            //提现失败
            Date now=new Date();
            MoneyWithdraw withdraw = moneyWithdrawMapper.selectByPrimaryKey(id);
            Long uid = withdraw.getUid();
            BigDecimal changeMoney = withdraw.getTotleMoney();

            UserMoney moneyInfo = userMoneyService.getMoneyInfo(uid);
            String oldLocking = moneyInfo.getLocking();
            String newLocking = IdUtil.randomUUID();
            BigDecimal oldMoney = moneyInfo.getMoney();
            BigDecimal newMoney = oldMoney.add(changeMoney);
            int i = userMoneyService.updateMoney(uid, oldLocking, newLocking, newMoney);
            compare(i == 0, "请重试!");
            MoneyLog log = new MoneyLog();
            log.setCreateTime(now);
            log.setUid(uid);
            log.setChangeMoney(changeMoney);
            log.setAfterMoney(newMoney);
            log.setBeforeMoney(oldMoney);
            log.setRemark(remark);
            log.setLocking(newLocking);
            log.setType(type.name());
            log.setTypeId(id.toString());
            moneyLogMapper.insert(log);
            MoneyWithdraw rec = new MoneyWithdraw();
            rec.setLocking(newLocking);
            rec.setId(id);
            rec.setStatus(2);
            rec.setRemark(remark);
            rec.setUpdateTime(now);
            moneyWithdrawMapper.updateByPrimaryKeySelective(rec);
        }

    }


    private void withdraw(MoneyTypeEnum type, MoneyWithdraw param) {
        Long uid = param.getUid();
        BigDecimal changeMoney = param.getTotleMoney();

        String remark=param.getRemark();
        compare(changeMoney.compareTo(BigDecimal.valueOf(0)) <= 0, "提现金额必须大于0");
        UserMoney money = userMoneyService.getMoneyInfo(uid);
        compare(money == null, "null is" + uid);
        compare(money.getMoney().compareTo(changeMoney) < 0, "提现超过[ " + money.getMoney() + " ],提现失败.");
        changeMoney = changeMoney.multiply(BigDecimal.valueOf(-1));
//-------------------

        Date now = new Date();


        String oldLocking = money.getLocking();
        String newLocking = IdUtil.randomUUID();
        BigDecimal oldMoney = money.getMoney();
        BigDecimal newMoney = oldMoney.add(changeMoney);
        int i = userMoneyService.updateMoney(uid, oldLocking, newLocking, newMoney);
        compare(i == 0, "请重试!");

        param.setStatus(0);
        param.setLocking(newLocking);
        moneyWithdrawMapper.insertSelective(param);
        Long id = param.getId();

        MoneyLog log = new MoneyLog();
        log.setCreateTime(now);
        log.setUid(uid);
        log.setChangeMoney(changeMoney);
        log.setAfterMoney(newMoney);
        log.setBeforeMoney(oldMoney);
        log.setRemark(remark);
        log.setLocking(newLocking);
        log.setType(type.name());
        log.setTypeId(id.toString());
        moneyLogMapper.insert(log);
    }


}
